﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace BusinessLogic
{
    public static class ChiTietCongNoNCC
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.ChiTietCongNoNCC[] Select(Entities.ChiTietCongNoNCC khachhang1)
        {
            Entities.ChiTietCongNoNCC[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietCongNoNCC.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.HanhDong, SqlDbType.NVarChar, 50).Value = khachhang1.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.MaNhaCungCap, SqlDbType.VarChar, 50).Value = khachhang1.MaNhaCungCap;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.MaKho, SqlDbType.VarChar, 50).Value = khachhang1.MaKho;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietCongNoNCC khachhang = new Entities.ChiTietCongNoNCC();
                    khachhang.MaKho = (dr[Common.Constants.ChiTietCongNoNCC.MaKho].ToString());
                    khachhang.DuNo = dr[Common.Constants.ChiTietCongNoNCC.DuNo].ToString();
                    khachhang.Deleted = bool.Parse(dr[Common.Constants.ChiTietCongNoNCC.Deleted].ToString());
                    khachhang.MaNhaCungCap = dr[Common.Constants.ChiTietCongNoNCC.MaNhaCungCap].ToString();
                    arr.Add(khachhang);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietCongNoNCC[0];
                }
                else
                {
                    list = new Entities.ChiTietCongNoNCC[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietCongNoNCC)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietCongNoNCC[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.ChiTietCongNoNCC khachhang)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietCongNoNCC.InsertUpdate;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.HanhDong, SqlDbType.NVarChar, 50).Value = khachhang.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.MaNhaCungCap, SqlDbType.VarChar, 50).Value = khachhang.MaNhaCungCap;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.MaKho, SqlDbType.VarChar, 50).Value = khachhang.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietCongNoNCC.DuNo, SqlDbType.Float).Value = khachhang.DuNo;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }
        #endregion
    }
}
#endregion
